package com.example.administrator.myapplication.guide;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.widget.Toast;

import com.example.administrator.myapplication.HomeActivity;

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        boolean mFirst = isFirstEnter(SplashActivity.this,SplashActivity.this.getClass().getName());
      //  Toast.makeText(this, mFirst + Toast.LENGTH_SHORT).show();
        if(mFirst)
            mHandler.sendEmptyMessageDelayed(SWITCH_GUIDACTIVITY,100);
        else
            mHandler.sendEmptyMessageDelayed(SWITCH_MAINACTIVITY,100);
        SharedPreferences sharedPreferences= this.getSharedPreferences(SHAREDPREFERENCES_NAME, MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(KEY_GUIDE_ACTIVITY, false).commit();
    }


    private static  final  String SHAREDPREFERENCES_NAME = "my_spr";
    private static  final  String KEY_GUIDE_ACTIVITY = "guide_activity";
    private boolean  isFirstEnter(Context context, String className){
        if(context==null|| className==null)
            return false;
        String mResultStr = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_WORLD_READABLE).getString(KEY_GUIDE_ACTIVITY, "");
        if(mResultStr.equalsIgnoreCase("false"))
            return false;
        else
            return true;
    }


    private final  static  int  SWITCH_MAINACTIVITY = 1000;
    private final  static  int  SWITCH_GUIDACTIVITY = 1001;
    public Handler mHandler = new Handler(){
        public void  handleMessage(Message msg) {
            switch(msg.what){
                case SWITCH_MAINACTIVITY:
                    Intent mIntent = new Intent();
                    mIntent.setClass(SplashActivity.this, HomeActivity.class);
                    SplashActivity.this.startActivity(mIntent);
                    SplashActivity.this.finish();
                    break;
                case SWITCH_GUIDACTIVITY:
                    mIntent = new Intent();
                    mIntent.setClass(SplashActivity.this, GuideActivity.class);
                    SplashActivity.this.startActivity(mIntent);
                    SplashActivity.this.finish();
                    break;
            }
            super.handleMessage(msg);
        }
    };
}
